//
//  LJYRecDetailViewController.m
//  WWLNetease
//
//  Created by 李锦耀 on 15/12/24.
//  Copyright © 2015年 Echo.Jobs. All rights reserved.
//

#import "LJYRecDetailViewController.h"
#import "LJYReadDetailModel.h"


@interface LJYRecDetailViewController ()<UIWebViewDelegate, UIScrollViewDelegate>

@property (nonatomic, retain) LJYReadDetailModel *readDeatilM;
@property (nonatomic, retain) UIWebView *myWebView;
@property (nonatomic, retain) UILabel *toptextLabel;
@property (nonatomic, retain) UILabel *topLabel;
@property (nonatomic, retain) UIView *webBrowserView;

@end

@implementation LJYRecDetailViewController



-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"changeFrame" object:nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(backAction)];
    [self creatWebView];
}

-(void)creatWebView
{
    self.myWebView = [[UIWebView alloc] init];
    [self.view addSubview:self.myWebView];
    [self.myWebView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
//        make.top.left.right.equalTo(self.view);
        
    }];
    
    self.myWebView.delegate = self;
    self.myWebView.scrollView.delegate = self;
    
    [self clearWebViewBackground:self.myWebView];
    
    self.myWebView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.myWebView];
    
    NSString *htmlStr = [NSString stringWithFormat:@"http://c.3g.163.com/nc/article/%@/full.html", self.loadId];
    [self loadDetailDataWithURLStr:htmlStr];

}

-(void)backAction
{
    [self.navigationController popViewControllerAnimated:YES];
    
}


- (void)loadDetailDataWithURLStr:(NSString *)urlStr
{
    // 网址
    NSString *url_string = [urlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    
    // AFN解析
    [AppTools getDataWithURL:url_string Block:^(id result) {
        
        // 取出数据
        NSDictionary *dic = (NSDictionary *)result;
        
        // 取出数据
        NSDictionary *reddic = [NSDictionary dictionary];
        reddic = [dic objectForKey:self.loadId];
        
        // 初始化
        self.readDeatilM = [[LJYReadDetailModel alloc] init];
        
        // KVC赋值
        [self.readDeatilM setValuesForKeysWithDictionary:reddic];
        
        // 获取内容
        [self loadMyWebView];

        
    } ErrorBlock:^(NSError *error) {
        
        
    }];
    
}
-(void) loadMyWebView
{
    // 添加图片
    [self loadMyWebViewImage];
    
    // 获取内容
    self.readDeatilM.body = [NSString stringWithFormat:@"<h2>%@</h2>%@\n", self.readDeatilM.title, self.readDeatilM.body];
    
    // 添加
    [self.myWebView loadHTMLString:self.readDeatilM.body baseURL:nil];
    
}
- (void)loadMyWebViewImage
{
    // 遍历
    for (NSInteger i = 0; i < self.readDeatilM.img.count; i++) {
        
        // 获取当前img字符串
        NSString *imgStr = [NSString stringWithFormat:@"<!--IMG#%ld-->", i];
        NSDictionary *dic = [self.readDeatilM.img objectAtIndex:i];
        
        //获取图片大小
        NSArray *array = [[dic objectForKey:@"pixel"] componentsSeparatedByString:@"*"];
        
        // 拆分获得x,n
        CGFloat width = [[array objectAtIndex:0] integerValue];
        width = (self.view.frame.size.width - 20) / width;
        CGFloat height = [[array objectAtIndex:1] integerValue];
        height = height * width;
        width = (self.view.frame.size.width - 20);
        
        // 过滤图片
        if ([[dic objectForKey:@"src"] isEqualToString:@"http://img2.cache.netease.com/3g/2015/9/29/20150929101602d08d5.png"]) {
            break;
        }
        
        
        // 拼接
        NSString *str = [NSString stringWithFormat:@"<img src='%@'  height='%f' width='%f' />\n",[dic objectForKey:@"src"], height, width];
        
        // 拼接内容
        self.readDeatilM.body = [self.readDeatilM.body stringByReplacingOccurrencesOfString:imgStr withString:str];
    }
}


- (void)clearWebViewBackground:(UIWebView *)webView
{
    UIWebView *web = webView;
    for (id view in web.subviews) {
        if ([view isKindOfClass:[UIScrollView class]]) {
            [view setBounces:NO];
        }
    }
}

-(void)backButton:(UIButton *)btn
{
//    NSLog(@"sss");
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
